﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.Exceptions;
using WmsApp.Service;
using WmsApp.StaticData.Unity;
using WmsApp.WmsService;

namespace WmsApp.Window
{
    public partial class FormProvider : BaseForm
    {
        const string Condition = " and OWNERNO like '%{0}%' and OWNERNAME like '%{1}%'  ";
        public FormProvider()
        {
            InitializeComponent();
            this.gridViewMain.CustomRowCellEdit += GridViewMainCustomRowCellEdit;
            QueryData(" and 1=2 ");
        }

        void GridViewMainCustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            if (e.Column == this.gridColumn1)
            {
                var customer = this.gridViewMain.GetRow(e.RowHandle) as Customer;
                if (customer != null)
                {
                    if (customer.WcfEntityState != EntityState.Added)
                    {
                        e.RepositoryItem = base.RepTextEditReadOnly;
                    }
                }
            }
        }


        public override string HeadText
        {
            get { return "供应商管理"; }
        }

        private void QueryData(string sql)
        {
            var client = EntityServiceFactory.GetEntityService<Customer, CustomerService>();
            var list = client.GetEntityList(sql);
            this.gridControlMain.DataSource = list;
        }


        private void CmdQuery_Click(object sender, EventArgs e)
        {
            QueryData(string.Format(Condition, this.textEditProviderCode.Text.Trim(),
                                                   this.textEditProviderName.Text.Trim()));
        }

        private void CmdNew_Click(object sender, EventArgs e)
        {
            var customer = Customer.CreateCustomer("请更改代码");
            if (this.gridControlMain.DataSource == null)
            {
                this.gridControlMain.DataSource = new List<Customer>();
            }
            ((List<Customer>)this.gridViewMain.DataSource).Add(customer);
            this.gridControlMain.RefreshDataSource();

            for (int idx = 0; idx < ((List<Customer>)this.gridViewMain.DataSource).Count; idx++)
            {
                var u = (Customer)this.gridViewMain.GetRow(idx);

                if (u == customer)
                {
                    this.gridViewMain.FocusedRowHandle = idx;
                }
            }
        }

        private void CmdSave_Click(object sender, EventArgs e)
        {
            try
            {
                var list = EntityControl.EntityFilterByState.GetNeedSaveEntity(this.gridViewMain.DataSource as List<Customer>);
                if (list.Count > 0)
                {
                    EntityServiceFactory.GetEntityService<Customer, CustomerService>().
                        SaveEntityList(list);
                    XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    SubjectFactory.GetSubject().Nofify();
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.ShowException(ex);
            }
        }

        private void CmdDelete_Click(object sender, EventArgs e)
        {
            try
            {
                var customer = this.gridViewMain.GetRow(this.gridViewMain.FocusedRowHandle) as Customer;
                if (customer != null)
                {
                    if (System.Windows.Forms.DialogResult.Yes == XtraMessageBox.Show(String.Format("你确实要删除当前记录{0}吗？", customer.OWNERNAME), "提问", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        Factory.WmsServiceFactory.GetServiceClient().DeleteCustomer(customer.OWNERNO);
                        ((List<Customer>)this.gridControlMain.DataSource).Remove(customer);
                        this.gridControlMain.RefreshDataSource();
                    }
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }
    }
}